<?php
namespace app\admin\logic;

use app\common\exception\LogicException;
use app\common\logic\RandCreateStrLogic;
use app\model\Factory;
use app\model\User;

/**
 * 猪厂类
 */
class FactoryLogic
{
    /**
     * @Description 列表
     * @author: zj
     * @param array $param
     * @time 2024/03/05 12:13:20
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function getList(array $param)
    {
        $model = new Factory();
        $query = $model->field('id,name,address,contacts,phone,create_time')
            ->where('delete_time',$model::IS_DELETE_NO);
        if ($param['name']) {
            $query->where('name','like','%'.trim($param['name']).'%');
        }

        return $query->order('id DESC')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

    public function add(string $name, string $address, string $contacts, string $phone)
    {
        //判断名称是否存在
        $factoryModel = new Factory();
        $exist = $factoryModel->checkNameIsExist($name);
        if ($exist) {
            throw new LogicException('名称已经存在');
        }

        //判断手机号是否注册了用户
        $userModel = new User();

        //创建key和密钥
        $createLogic = new RandCreateStrLogic();
        $key = $this->createKey($createLogic, $factoryModel);
        $secret = $createLogic->create(32);

        $id = $factoryModel->insertGetId([
            'name' => $name,
            'key' => $key,
            'secret' => $secret,
            'address' => $address,
            'contacts' => $contacts,
            'phone' => $phone,
            'create_time' => request()->time()
        ]);
        if (!$id) {
            throw new LogicException('新增失败');
        }

        //写入缓存
        $factoryInfo = [
            'id' => $id,
            'key' => $key,
            'secret' => $secret
        ];
        cache()->set($key, json_encode($factoryInfo, JSON_UNESCAPED_UNICODE));

        return [
            'id' => $id,
            'key' => $key,
            'secret' => $secret
        ];
    }

    /**
     * @Description 详情
     * @author: zj
     * @param int $id
     * @time 2024/03/05 12:21:12
     * @return array
     */
    public function detail(int $id)
    {
        $model = new Factory();
        $info = $model->field('id,key,secret,name,address,contacts,phone,create_time')
            ->where('id',$id)
            ->where('delete_time',$model::IS_DELETE_NO)
            ->find();
        if (!$info) {
            throw new LogicException('数据不存在');
        }

        return $info->toArray();
    }

    /**
     * @Description 下拉选择
     * @author: zj
     * @param array $param
     * @time 2024/03/05 15:37:52
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function selectList(array $param)
    {
        $model = new Factory();
        $query = $model->field('id,name')
            ->where('delete_time',$model::IS_DELETE_NO);
        if ($param['name']) {
            $query->where('name','like','%'.trim($param['name']).'%');
        }

        return $query->order('id DESC')
            ->paginate([
                'list_rows' => $param['page_size'],
                'page' => $param['page']
            ])
            ->toArray();
    }

    /**
     * 创建key
     * @author 吴伟亮
     * @param RandCreateStrLogic $createLogic
     * @param Factory $factoryModel
     * @return void
     */
    public function createKey(RandCreateStrLogic $createLogic, Factory $factoryModel)
    {
        $key = $createLogic->create(32);
        $exist = $factoryModel->checkKeyIsExist($key);
        if ($exist) {
            $key = $this->createKey($createLogic, $factoryModel);
        }

        return $key;
    }

}
